//
//  CLPhoneModeFenceSettingCell.m
//  togetherLBS
//
//  Created by louis on 15/9/18.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLPhoneModeFenceSettingCell.h"

@interface CLPhoneModeFenceSettingCell()
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *fenceNameLabel;
@property (nonatomic, weak) UISwitch *onSwitch;
@end

@implementation CLPhoneModeFenceSettingCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *cellID = @"fenceSettingCell";
    
    CLPhoneModeFenceSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[CLPhoneModeFenceSettingCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        ;
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        // 初始化操作, 添加子控件
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        titleLabel.backgroundColor = [UIColor clearColor];
        
        UILabel *fenceNameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:fenceNameLabel];
        self.fenceNameLabel = fenceNameLabel;
        fenceNameLabel.backgroundColor = [UIColor clearColor];
        
        UISwitch *onSwitch = [[UISwitch alloc] init];
        [self.contentView addSubview:onSwitch];
        self.onSwitch = onSwitch;
    }
    return self;
}

- (void)setItem:(CLPhoneModeFenceItem *)item
{
    _item = item;
    self.titleLabel.text = item.title;
    self.fenceNameLabel.text = item.fenceName;
    
    if (![self.fenceNameLabel.text isEqualToString:NSLocalizedString(@"NotSet", nil)]) {
        self.fenceNameLabel.textColor = [UIColor colorWithRed:0 green:11/255.0 blue:1.0 alpha:1.0];
    } else {
        self.fenceNameLabel.textColor = [UIColor redColor];
    }
    
    self.onSwitch.on = item.isOn;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat height = self.frame.size.height;
    CGFloat width = self.contentView.frame.size.width;
    
    CGFloat titleX = 17;
    CGFloat titleY = 0;
    CGFloat titleW = 100;
    CGFloat titleH = height;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    CGFloat nameX = titleX + titleW + 5;
    CGFloat nameY = 0;
    CGFloat nameW = 100;
    CGFloat nameH = height;
    self.fenceNameLabel.frame = CGRectMake(nameX, nameY, nameW, nameH);
    
    
    CGFloat onW = self.onSwitch.frame.size.width;
    CGFloat onH = self.onSwitch.frame.size.height;
    CGFloat onX = width - onW;
    CGFloat onY = (height - onH) * 0.5;
    self.onSwitch.frame = CGRectMake(onX, onY, 0, 0);
}

@end
